package com.cgbl.cg.adapter

import android.app.Activity
import android.content.res.Configuration
import android.view.View
import android.widget.ImageView
import com.cgbl.cg.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.model.WorksResourceBean
import com.robot.base.view.SampleCoverVideo
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.Debuger
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

class VideoAdapter : BaseQuickAdapter<WorksResourceBean, BaseViewHolder>(R.layout.app_item_video) {

    private var curPlayer: StandardGSYVideoPlayer? = null
    private var itemPlayer: StandardGSYVideoPlayer? = null

    var orientationUtils: OrientationUtils? = null

    var isPlay = false

    var isFull = false

    override fun convert(holder: BaseViewHolder, item: WorksResourceBean) {
//        final String url = "http://111.198.24.133:83/yyy_login_server/pic/YB059284/97778276040859/1.mp4";
        val gsyVideoPlayer = holder.getView<SampleCoverVideo>(R.id.video_item_player)

        //默认缓存路径
        //使用lazy的set可以避免滑动卡的情况存在
        gsyVideoPlayer.setUpLazy(item.filePath, true, null, null, "")

        //增加title
        gsyVideoPlayer.titleTextView.visibility = View.GONE

        //设置返回键

        //设置返回键
        gsyVideoPlayer.backButton.visibility = View.GONE

        //设置全屏按键功能

        //设置全屏按键功能
        gsyVideoPlayer.fullscreenButton
            .setOnClickListener(View.OnClickListener { resolveFullBtn(gsyVideoPlayer) })
        gsyVideoPlayer.isRotateViewAuto = !getListNeedAutoLand()
        gsyVideoPlayer.isLockLand = !getListNeedAutoLand()
        gsyVideoPlayer.playTag = VideoAdapter::class.simpleName
        //gsyVideoPlayer.c(true);
        //gsyVideoPlayer.c(true);
        gsyVideoPlayer.isReleaseWhenLossAudio = false
        gsyVideoPlayer.isRotateWithSystem = false
        gsyVideoPlayer.isAutoFullWithSize = true
        gsyVideoPlayer.isShowFullAnimation = !getListNeedAutoLand()
        gsyVideoPlayer.setIsTouchWiget(false)
        gsyVideoPlayer.shrinkImageRes = R.mipmap.video_enlarge
        gsyVideoPlayer.enlargeImageRes = R.mipmap.video_enlarge

        gsyVideoPlayer.findViewById<ImageView>(R.id.iv_need).setOnClickListener {
            GSYVideoManager.instance().isNeedMute = !GSYVideoManager.instance().isNeedMute
            if(GSYVideoManager.instance().isNeedMute){
                gsyVideoPlayer.findViewById<ImageView>(R.id.iv_need).setImageResource(R.mipmap.app_video_need_un)
            }else{
                gsyVideoPlayer.findViewById<ImageView>(R.id.iv_need).setImageResource(R.mipmap.app_video_need)
            }
        }
        //循环
        //gsyVideoPlayer.setLooping(true);
        //循环
        //gsyVideoPlayer.setLooping(true);
        gsyVideoPlayer.isNeedLockFull = false

        gsyVideoPlayer.speed = 1f;

        //gsyVideoPlayer.setSpeed(2);
        gsyVideoPlayer.playPosition = holder.adapterPosition

        gsyVideoPlayer.setVideoAllCallBack(object : GSYSampleCallBack() {
            override fun onClickStartIcon(url: String, vararg objects: Any) {
                super.onClickStartIcon(url, *objects)
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_stop)
            }

            override fun onClickResumeFullscreen(url: String?, vararg objects: Any?) {
                super.onClickResumeFullscreen(url, *objects)
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_stop)
            }

            override fun onClickResume(url: String?, vararg objects: Any?) {
                super.onClickResume(url, *objects)
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_stop)
            }

            override fun onClickStop(url: String?, vararg objects: Any?) {
                super.onClickStop(url, *objects)
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_play)
            }

            override fun onClickStopFullscreen(url: String?, vararg objects: Any?) {
                super.onClickStopFullscreen(url, *objects)
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_play)
            }

            override fun onPrepared(url: String, vararg objects: Any) {
                super.onPrepared(url, *objects)
                Debuger.printfLog("onPrepared")
                val full: Boolean =
                    gsyVideoPlayer.currentPlayer.isIfCurrentIsFullscreen
                if (!gsyVideoPlayer.currentPlayer.isIfCurrentIsFullscreen) {
                    GSYVideoManager.instance().isNeedMute = true
                }
                if (gsyVideoPlayer.currentPlayer.isIfCurrentIsFullscreen) {
                    GSYVideoManager.instance().setLastListener(gsyVideoPlayer)
                }
                curPlayer = objects[1] as StandardGSYVideoPlayer
                itemPlayer = gsyVideoPlayer
                isPlay = true

                if (getListNeedAutoLand()) {
                    //重力全屏工具类
                    initOrientationUtils(gsyVideoPlayer, full)
                    this@VideoAdapter.onPrepared()
                }
            }

            override fun onQuitFullscreen(url: String, vararg objects: Any) {
                super.onQuitFullscreen(url, *objects)
                isFull = false
                GSYVideoManager.instance().isNeedMute = true
                if (getListNeedAutoLand()) {
                    this@VideoAdapter.onQuitFullscreen()
                }
            }

            override fun onEnterFullscreen(url: String, vararg objects: Any) {
                super.onEnterFullscreen(url, *objects)
                GSYVideoManager.instance().isNeedMute = false
                isFull = true
                gsyVideoPlayer.currentPlayer.titleTextView.text = objects[0] as String
            }

            override fun onAutoComplete(url: String, vararg objects: Any) {
                super.onAutoComplete(url, *objects)
                curPlayer = null
                itemPlayer = null
                isPlay = false
                isFull = false
                gsyVideoPlayer.findViewById<ImageView>(R.id.video_state).setImageResource(R.mipmap.app_video_play)
                if (getListNeedAutoLand()) {
                    this@VideoAdapter.onAutoComplete()
                }
            }
        })
    }

    /**
     * 全屏幕按键处理
     */
    private fun resolveFullBtn(standardGSYVideoPlayer: StandardGSYVideoPlayer) {
        if (getListNeedAutoLand() && orientationUtils != null) {
            resolveFull()
        }
        standardGSYVideoPlayer.startWindowFullscreen(context, false, true)
    }

    fun clearCache() {
        if (curPlayer != null) {
            curPlayer?.currentPlayer?.clearCurrentCache()
        }
    }

//    fun isFull(): Boolean {
//        return isFull
//    }

    /**************************支持全屏重力全屏的部分**************************/

    /**************************支持全屏重力全屏的部分 */
    /**
     * 列表时是否需要支持重力旋转
     *
     * @return 返回true为支持列表重力全屏
     */
    fun getListNeedAutoLand(): Boolean {
        return true
    }

    private fun initOrientationUtils(
        standardGSYVideoPlayer: StandardGSYVideoPlayer,
        full: Boolean
    ) {
        orientationUtils = OrientationUtils(context as Activity, standardGSYVideoPlayer)
        //是否需要跟随系统旋转设置
        //orientationUtils.setRotateWithSystem(false);
        orientationUtils?.isEnable = false
        orientationUtils?.isLand = if (full) 1 else 0
    }

    private fun resolveFull() {
        if (getListNeedAutoLand() && orientationUtils != null) {
            //直接横屏
            // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
            // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
            orientationUtils?.resolveByClick()
        }
    }

    private fun onQuitFullscreen() {
        // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
        // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
        if (orientationUtils != null) {
            orientationUtils?.backToProtVideo()
        }
    }

    fun onAutoComplete() {
        if (orientationUtils != null) {
            orientationUtils?.setEnable(false)
            orientationUtils?.releaseListener()
            orientationUtils = null
        }
        isPlay = false
    }

    fun onPrepared() {
        if (orientationUtils == null) {
            return
        }
        //开始播放了才能旋转和全屏
        orientationUtils?.setEnable(true)
    }

    /**
     * orientationUtils 和  detailPlayer.onConfigurationChanged 方法是用于触发屏幕旋转的
     */
    fun onConfigurationChanged(activity: Activity?, newConfig: Configuration?) {
        //如果旋转了就全屏
        if (isPlay && itemPlayer != null && orientationUtils != null) {
            itemPlayer?.onConfigurationChanged(activity, newConfig, orientationUtils, false, true)
        }
    }

//    fun getOrientationUtils(): OrientationUtils? {
//        return orientationUtils
//    }


    fun onBackPressed() {
        // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
        // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
        if (orientationUtils != null) {
            orientationUtils?.backToProtVideo()
        }
    }

    fun onDestroy() {
        if (isPlay && curPlayer != null) {
            curPlayer?.currentPlayer?.release()
        }
        if (orientationUtils != null) {
            orientationUtils?.releaseListener()
            orientationUtils = null
        }
    }


}